package com.ysy.logistics.controller.stock;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysy.common.resp.ResultData;
import com.ysy.logistics.entities.stock.GoodsCapacity;
import com.ysy.logistics.myannotation.OperateLog;
import com.ysy.logistics.service.stock.GoodsCapacityService;
import com.ysy.logistics.util.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/31 21:58
 **/
@Slf4j
@RestController
@RequestMapping(value = "/goodsCapacity",name = "商品规格")
@Tag(name = "商品规格",description = "goodsCapacity表的CRUD")
@CrossOrigin
public class GoodsCapacityController {

    @Resource
    private GoodsCapacityService goodsCapacityService;

    @PostMapping(value = "/listByPage")
    @Operation(summary = "分页查询商品规格",description = "分页查询商品规格")
    @OperateLog(value = "查询商品规格")
    public ResultData<Page<GoodsCapacity>> listByPage(@RequestBody PageInfo<GoodsCapacity> pageInfo){
        return ResultData.success(goodsCapacityService.listByPage(pageInfo));
    }

    @PostMapping(value = "/save")
    @Operation(summary = "保存/更新商品规格",description = "保存/更新商品规格")
    @OperateLog(value = "保存/更新商品规格")
    public ResultData<GoodsCapacity> save(@Validated @RequestBody GoodsCapacity goodsCapacity){
        log.info("保存/更新商品规格:{}",goodsCapacity);
        goodsCapacityService.saveOrUpdate(goodsCapacity);
        return ResultData.success();
    }

    @GetMapping(value = "/changeStatus/{id}")
    @Operation(summary = "修改商品规格状态",description = "修改商品规格状态")
    @OperateLog(value = "修改商品规格状态")
    public ResultData<GoodsCapacity> changeStatus(@PathVariable("id") String id){
        GoodsCapacity goodsCapacity = goodsCapacityService.getById(id);
        if (Objects.isNull(goodsCapacity)){
            return ResultData.fail("商品规格不存在");
        }
        GoodsCapacity update = new GoodsCapacity();
        update.setId(id);
        if (StringUtils.equals(goodsCapacity.getStatus(),"0")){
            update.setStatus("1");
        }else {
            update.setStatus("0");
        }
        goodsCapacityService.updateById(update);
        return ResultData.success();
    }

    @GetMapping(value = "/delete/{id}")
    @Operation(summary = "删除商品规格",description = "删除商品规格")
    @OperateLog(value = "删除商品规格")
    public ResultData<GoodsCapacity> delete(@PathVariable("id") String id){
        GoodsCapacity goodsCapacity = goodsCapacityService.getById(id);
        if (Objects.isNull(goodsCapacity)){
            return ResultData.fail("商品规格不存在");
        }
        goodsCapacityService.removeById(id);
        return ResultData.success();
    }

    @PostMapping(value = "/updateById")
    @Operation(summary = "修改商品规格",description = "修改商品规格")
    @OperateLog(value = "修改商品规格")
    public ResultData<GoodsCapacity> updateById(@RequestBody GoodsCapacity goodsCapacity){
        log.info("修改商品规格:{}",goodsCapacity);
        if (Objects.isNull(goodsCapacity.getId())){
            return ResultData.fail("商品规格id不能为空");
        }
        GoodsCapacity byId = goodsCapacityService.getById(goodsCapacity.getId());
        if (Objects.isNull(byId)){
            return ResultData.fail("商品规格不存在");
        }
        //暂时只允许修改容量和排序
        GoodsCapacity update = new GoodsCapacity();
        update.setId(goodsCapacity.getId());
        if (Objects.nonNull(goodsCapacity.getCapacity())){
            update.setCapacity(goodsCapacity.getCapacity());
        }
        if (Objects.nonNull(goodsCapacity.getNum())){
            Integer num = goodsCapacity.getNum();
            update.setNum(num);
        }
        goodsCapacityService.updateById(update);
        return ResultData.success();
    }


}
